﻿using GoodDotNet.IService;
using GoodDotNet.Core.Entity;
using GoodDotNet.Core.Model;
using GoodDotNet.Core.Data;

namespace GoodDotNet.Service
{
    public class AdminModuleService :BaseService,AdminModuleIService
    {
        public AdminModuleService(UnitOfWork _unitOfWork) : base(_unitOfWork)
        {
        }

        public void Add(ParInsertAdminModuleModel pars,int uid)
		{
			AdminModule entity=new AdminModule();
			
			entity.Name=pars.name;
			entity.Description=pars.description;
			entity.Uid=uid;
			entity.Path=pars.path;
			entity.Sn=pars.sn;
			entity.ParentId=pars.parentId;
			entity.Icon=pars.icon;
            entity.Deleted = 0;
            entity.InsertTime = DateTime.Now;
            entity.Btns = pars.btns;
            entity.Redirect=pars.redirect;
            entity.Hidden=pars.hidden;
            entity.Component = pars.component;
            entity.Title = pars.title;
          
			 
		    _unitOfWork.AdminModuleRepository.Add(entity);
		    _unitOfWork.SaveChanges();
		
		}
		
				
		public void Update(ParUpdateAdminModuleModel pars,int uid)
		{
            var entity=_unitOfWork.AdminModuleRepository.Get(a=>a.Id==pars.id);
            if (entity != null)
            {

                entity.Name = pars.name;
                entity.ParentId = pars.parentId;
                entity.Component = pars.component;
                entity.Icon = pars.icon;
                entity.Hidden = pars.hidden;
                entity.Redirect = pars.redirect;
                entity.Sn = pars.sn;
                entity.Title = pars.title;
                entity.Description = pars.description;
                entity.UpdateTime = DateTime.Now;
                entity.Uid = uid;
                entity.Path = pars.path;
                entity.Btns = pars.btns;
                entity.Redirect = pars.redirect;

                _unitOfWork.AdminModuleRepository.Update(entity);

                _unitOfWork.SaveChanges();
            }
		}
		
		
				
		public AdminModule? GetOne(ParGetOneAdminModuleModel pars)
		{
			
			var data = _unitOfWork.AdminModuleRepository.Get(a=>a.Id ==pars.id);
			
			return data;
		  
		}
		
				
		public List<DataGetListAdminModuleModel>? GetList( ParGetListAdminModuleModel pars )
		{
			
			//var list = _unitOfWork.AdminModuleRepository.GetList(a=>a.Id == pars.parent_id,t=>new DataGetListAdminModuleModel{[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object],[object Object]=t.[object Object]});
			
			return null;
		  
		}
				
		public int Delete(ParDeleteAdminModuleModel pars,int uid)
		{
				System.FormattableString sql = $"update admin_module set deleted=1,uid={uid} where  id={pars.id}";
				return _unitOfWork.AdminModuleRepository.ExecuteSql(sql);
		}
        public void UpdataRolePermission(int role, ParUpdateRoleModule[] pars,int uid)
        {
            var modules =_unitOfWork.AdminRoleModuleRepository.GetList(a=>a.RoleId==role);
            foreach (var par in pars)
            {
                AdminRoleModule module =modules.Where(a=>a.ModuleId==par.moduleId).FirstOrDefault();
                if (module != null)
                {
                    module.Btns = par.btns;
                }
                else
                {
                    AdminRoleModule m=new AdminRoleModule();
                    m.ModuleId=par.moduleId;
                    m.Uid = uid;
                    m.RoleId = role;
                    _unitOfWork.AdminRoleModuleRepository.Add(m);
                }
            }
            foreach(var module in modules)
            {
                var m=pars.Where(a=>a.moduleId==module.ModuleId).FirstOrDefault();
                if(m == null)
                {
                    _unitOfWork.AdminRoleModuleRepository.Remove(module.ModuleId,role);
                }
            }
            _unitOfWork.SaveChanges();
        }
        public void UpdataModulePermission(int ModuleID, ParUpdateModuleRole[] pars, int uid)
        {
            var roles = _unitOfWork.AdminRoleModuleRepository.GetList(a=>a.ModuleId==ModuleID);
            foreach (var par in pars)
            {
                AdminRoleModule module = roles.Where(a => a.RoleId == par.RoleId).FirstOrDefault();
                if (module != null)
                {
                    module.Btns = par.btns;
                }
                else
                {
                    AdminRoleModule m = new AdminRoleModule();
                    m.ModuleId = ModuleID;
                    m.Uid = uid;
                    m.RoleId = par.RoleId;
                    _unitOfWork.AdminRoleModuleRepository.Add(m);
                }
            }
            foreach (var role in roles)
            {
                var m = pars.Where(a => a.RoleId == role.RoleId).FirstOrDefault();
                if (m == null)
                {
                    _unitOfWork.AdminRoleModuleRepository.Remove(ModuleID, role.RoleId);
                }
            }
            _unitOfWork.SaveChanges();
        }
    }
}
